package edu.msse.signup;

import java.util.List;

import org.springframework.orm.jpa.support.JpaDaoSupport;

public class VolunteerServiceImpl extends JpaDaoSupport implements VolunteerService {
	
	public VolunteerServiceImpl() {
		super();
	}

	public void fillPositionTimeSlot(Position position, TimeSlot timeSlot,
			Users user) {
		
		if (timeSlot != null){
			TimeSlotService ts = new TimeSlotService();
			ts.addUser(timeSlot, user);
		}else {
			PositionService ps = new PositionService();
			ps.addUser(position, user);
		}
	}

    @SuppressWarnings("unchecked")
	public List getOpenPositionsAndTimeSlots(Event event) {
    	return getJpaTemplate().find(
    			 "select p from Position p, TimeSlot t, Event e where e = ? and (" +
                 "(size(p.users) < p.numberOfVolunteers and p in elements(e.positions)) or " +
                 "(p = t.position and size(t.users) < t.numberOfVolunteers and p in elements(e.positions)))", event);
    }

    @SuppressWarnings("unchecked")
	public List getFilledPositionsAndTimeSlots(Event event){
		return getJpaTemplate().find(
                "select p, t from TimeSlot t, Position p, Event e where e = ? and (" +
                "(size(p.users) >= p.numberOfVolunteers and p in elements(e.positions)) or " +
                "(p = t.position and size(t.users) >= t.numberOfVolunteers and p in elements(e.positions)))", event);
		
	}

}
